﻿
Public Class Indirizzo

    Public Property PK_Indirizzo As Integer

    Public Property Strada As String

    Public Property Civico As String

    Public Property Comune As String

    Public Property Provincia As String

    Public Property CAP As String

    Public Property Presso As String

    Public Property Deleted As String

    Public ReadOnly Property DefinizioneCompleta
        Get
            Dim strRet As String = ""
            If Not String.IsNullOrEmpty(Strada) Then strRet &= Strada
            If Not String.IsNullOrEmpty(Civico) Then strRet &= " n. " & Civico
            If Not String.IsNullOrEmpty(Comune) Then strRet &= " " & Comune
            If Not String.IsNullOrEmpty(Provincia) Then strRet &= " (" & Provincia & ")"
            If Not String.IsNullOrEmpty(Presso) Then strRet &= " Presso " & Presso
            Return strRet
        End Get
    End Property

    Public Sub LoadFromPk(objpk As Integer)

        Dim sql As String = "SELECT * FROM Indirizzo WHERE PK_Indirizzo = " & objpk
        'Throw New Exception(sql)
        Dim ds As New DataSet
        Dim dbO As New DBObject
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_Indirizzo = objpk
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Strada")) Then
                Strada = ds.Tables(0).Rows(0).Item("Strada").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Civico")) Then
                Civico = ds.Tables(0).Rows(0).Item("Civico").ToString
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Comune")) Then
                Comune = ds.Tables(0).Rows(0).Item("Comune")
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Provincia")) Then
                Provincia = ds.Tables(0).Rows(0).Item("Provincia")
            End If

            If Not IsDBNull(ds.Tables(0).Rows(0).Item("CAP")) Then
                CAP = ds.Tables(0).Rows(0).Item("CAP").ToString
            End If
            If Not IsDBNull(ds.Tables(0).Rows(0).Item("Presso")) Then
                Presso = ds.Tables(0).Rows(0).Item("Presso").ToString
            End If

        End If
    End Sub

    Public Function Salva() As Integer
        If PK_Indirizzo = 0 Then
            Dim dbo As New DBObject
            Dim sql As String = "INSERT INTO Indirizzo (Strada, Civico, Comune, Provincia, CAP, Presso, Deleted) VALUES( " & _
                                " '" & Strada.Replace("'", "''") & "'," & _
                                " '" & Civico & "'," & _
                                " '" & Comune.Replace("'", "''") & "'," & _
                                " '" & Provincia & "'," & _
                                " '" & CAP & "'," & _
                                " '" & Presso.Replace("'", "''") & "', 0)"

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then
                sql = "SELECT MAX (PK_Indirizzo) FROM Indirizzo"
                dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
                Return dbo.ExecuteScalar(sql, Nothing, CommandType.Text)
            Else
                Return 0
            End If
        Else
            Dim dbo As New DBObject
            Dim sql As String = "UPDATE Indirizzo SET " & _
                                " Strada= '" & Strada.Replace("'", "''") & "'" & _
                                " ,Civico= '" & Civico & "'" & _
                                " ,Comune= '" & Comune.Replace("'", "''") & "'" & _
                                " ,Provincia= '" & Provincia & "'" & _
                                " ,Cap= '" & CAP & "'" & _
                                " ,Presso= '" & Presso.Replace("'", "''") & "'" & _
                                " WHERE PK_Indirizzo = " & PK_Indirizzo

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                Return PK_Indirizzo
            Else
                Return 0
            End If

        End If
    End Function

End Class
